<template>
    <el-dialog
            :title="!dataForm.id ? '新增' : '修改'"
            :close-on-click-modal="false"
            :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
                            <el-form-item label="主键ID" prop="id">
                        <el-input v-model.number="dataForm.id" placeholder="主键ID"></el-input>
                </el-form-item>
                <el-form-item label="客户ID" prop="customerId">
                        <el-input v-model.number="dataForm.customerId" placeholder="客户ID"></el-input>
                </el-form-item>
                <el-form-item label="客户行为数据" prop="behaviorData">
                        <el-input v-model="dataForm.behaviorData" placeholder="客户行为数据"></el-input>
                </el-form-item>
                <el-form-item label="数据指标" prop="dataIndicator">
                        <el-input v-model="dataForm.dataIndicator" placeholder="数据指标"></el-input>
                </el-form-item>
                <el-form-item label="风险预警阈值" prop="riskThreshold">
                        <el-input v-model="dataForm.riskThreshold" placeholder="风险预警阈值"></el-input>
                </el-form-item>
                <el-form-item label="预警状态（0：未预警，1：已预警）" prop="warningStatus">
                        <el-input v-model="dataForm.warningStatus" placeholder="预警状态（0：未预警，1：已预警）"></el-input>
                </el-form-item>
                <el-form-item label="预警时间" prop="warningTime">
                        <el-date-picker
                                v-model="dataForm.warningTime"
                                type="datetime"
                                placeholder="预警时间">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="客户群体ID" prop="groupId">
                        <el-input v-model.number="dataForm.groupId" placeholder="客户群体ID"></el-input>
                </el-form-item>
                <el-form-item label="群体关联性评分" prop="correlationScore">
                        <el-input v-model="dataForm.correlationScore" placeholder="群体关联性评分"></el-input>
                </el-form-item>
                <el-form-item label="宏观经济因素" prop="macroeconomicFactor">
                        <el-input v-model="dataForm.macroeconomicFactor" placeholder="宏观经济因素"></el-input>
                </el-form-item>
                <el-form-item label="市场趋势" prop="marketTrend">
                        <el-input v-model="dataForm.marketTrend" placeholder="市场趋势"></el-input>
                </el-form-item>
                <el-form-item label="营销策略调整" prop="strategyAdjustment">
                        <el-input v-model="dataForm.strategyAdjustment" placeholder="营销策略调整"></el-input>
                </el-form-item>
                <el-form-item label="创建时间" prop="createdAt">
                        <el-date-picker
                                v-model="dataForm.createdAt"
                                type="datetime"
                                placeholder="创建时间">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="更新时间" prop="updatedAt">
                        <el-date-picker
                                v-model="dataForm.updatedAt"
                                type="datetime"
                                placeholder="更新时间">
                        </el-date-picker>
                </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
    export default {
      data () {
        return {
          visible: false,
          dataForm: {
            id: null,
            customerId: null,
            behaviorData: null,
            dataIndicator: null,
            riskThreshold: null,
            warningStatus: null,
            warningTime: null,
            groupId: null,
            correlationScore: null,
            macroeconomicFactor: null,
            marketTrend: null,
            strategyAdjustment: null,
            createdAt: null,
            updatedAt: null
          },
          dataRule: {
            id: [
                        { required: true, message: '主键ID不能为空', trigger: 'blur' }
            ],
            customerId: [
                        { required: true, message: '客户ID不能为空', trigger: 'blur' }
            ],
            behaviorData: [
                        { required: true, message: '客户行为数据不能为空', trigger: 'blur' }
            ],
            dataIndicator: [
                        { required: true, message: '数据指标不能为空', trigger: 'blur' }
            ],
            riskThreshold: [
                        { required: true, message: '风险预警阈值不能为空', trigger: 'blur' }
            ],
            warningStatus: [
                        { required: true, message: '预警状态（0：未预警，1：已预警）不能为空', trigger: 'blur' }
            ],
            warningTime: [
                        { required: true, message: '预警时间不能为空', trigger: 'blur' }
            ],
            groupId: [
                        { required: true, message: '客户群体ID不能为空', trigger: 'blur' }
            ],
            correlationScore: [
                        { required: true, message: '群体关联性评分不能为空', trigger: 'blur' }
            ],
            macroeconomicFactor: [
                        { required: true, message: '宏观经济因素不能为空', trigger: 'blur' }
            ],
            marketTrend: [
                        { required: true, message: '市场趋势不能为空', trigger: 'blur' }
            ],
            strategyAdjustment: [
                        { required: true, message: '营销策略调整不能为空', trigger: 'blur' }
            ],
            createdAt: [
                        { required: true, message: '创建时间不能为空', trigger: 'blur' }
            ],
            updatedAt: [
                        { required: true, message: '更新时间不能为空', trigger: 'blur' }
            ]
          }
        }
      },
      methods: {
        init (id) {
          this.dataForm.id = id || 0
          this.visible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
            this.dataForm.id = id || 0
            if (this.dataForm.id) {
              this.$http({
                url: this.$http.adornUrl(`/app/risk_management/info/${this.dataForm.id}`),
                method: 'get',
                params: this.$http.adornParams()
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.dataForm = data.appriskmanagement
                }
              })
            }
          })
        },
            // 表单提交
        dataFormSubmit () {
          this.$refs['dataForm'].validate((valid) => {
            if (valid) {
              this.$http({
                url: this.$http.adornUrl(`/app/risk_management/${!this.dataForm.id ? 'save' : 'update'}`),
                method: 'post',
                data: this.$http.adornData(this.dataForm)
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                      this.visible = false
                      this.$emit('refreshDataList')
                    }
                  })
                } else {
                  this.$message.error(data.msg)
                }
              })
            }
          })
        }
      }
    }
</script>